package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.Result;
import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.DeviceDao;
import edu.ynu.se.xiecheng.achitectureclass.dao.GreenhouseDao;
import edu.ynu.se.xiecheng.achitectureclass.dto.GreenhouseObject;
import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.service.GreenhouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "大棚实体的控制器")
@RestController
@RequestMapping("/greenhouse")
public class GreenhouseController extends LogicController<GreenhouseService, GreenhouseDao, Greenhouse, Long> {

    @Resource
    private DeviceDao deviceDao;
    public GreenhouseController(@Autowired GreenhouseService service) {
        super(service);
    }

    @RequestMapping("/addDevice")
    @ApiOperation("添加设备到大棚")
    public Result addDevice(Long device, Long greenhouse) {
       return getService().addDevice(device, greenhouse);
    }

    @GetMapping("/getGreenhouse")
    @ApiOperation("获取大棚信息")
    public GreenhouseObject getGreenhouse() {
        Greenhouse greenhouse = getService().getAll().get(0);
        return new GreenhouseObject(greenhouse);
    }


    @GetMapping("/getUnbindDevices")
    @ApiOperation("获取未绑定的设备")
    public List<Device> getUnbindDevices() {
        List<Device> all = deviceDao.findAll();
        return all.stream().filter(o->o.getGreenhouse()==null).collect(Collectors.toList());
    }

}
